import api from '~api/index'
import util from '~src/lib/util'
export default {
  state: {
    gasNoInfo: {},   // 油枪信息
    gasstationInfo: {}, //油站信息
    RealPrice: {},   // 油价
    Couponlist:[],   // 优惠价
    Goodslist:[],    // 非油品
    payinfo: {},     // 支付信息
    userInfo: {},      // 用户信息
    commentShow: {}
  },
  mutations: {
    ['setGasNoInfo'] (state, data) {
      state.gasNoInfo = data
    },
    ['setGasstationInfo'] (state, data) {
      state.gasstationInfo = data
    },
    ['setRealPrice'] (state, data) {
      state.RealPrice = data
    },
    ['setMemberInfoByGsId'] (state, data) {
      state.userInfo = data
    },
    ['setCouponListByGs'] (state, data) {
      state.Couponlist = data
    },
    ['setGoodslist'] (state, data) {
      state.Goodslist = data
    },
    ['setPayinfo'] (state, data) {
      state.payinfo = data
    },
    ['setComment'] (state, data) {
      state.commentShow = data
    }
  },
  actions: {
    async ['getGasstationInfo'] ({commit}, id) {
      const { data: { data, code,msg } } = await api.post('/v1/getGasstationInfo',
        {openid: util.localItem.get('openid'), gsId: id})
      if (data && code === '200') {
        commit('setGasstationInfo', data)
      } else {
        commit('setGasstationInfo', {code:code, msg:msg})
      }
    },
    // 获取油枪信息
    async ['getGasNoInfo'] ({commit}, config) {
      const { data: { data, code,msg } } = await api.post('/v1/getGasNoInfo',
        {openid: util.localItem.get('openid'), gsId: config.id,gsGunNo:config.gsGunNo})
      if (data && code === '200') {
        commit('setGasNoInfo', data)
      } else {
        commit('setGasNoInfo', {code:code, msg:msg})
      }
    },
    // 获取优惠价格
    async ['getRealPrice'] ({commit}, config) {
      const { data: { data, code } } = await api.post('/v1/getRealPrice',
        {gsId: config.id,gsNoId:config.gsGunNo,gsPrice:config.gsPrice})
      if (data && code === '200') {
        commit('setRealPrice', data)
      }
    },
    // 获取优惠劵
    async ['getCouponListByGs'] ({commit}, config) {
      const { data: { data, code } } = await api.post('/v1/getCouponListByGs',
        {openid: util.localItem.get('openid'),gsId: config.id,gsPrice:config.gsPrice,discountPrice:config.discountPrice})
      if (data && code === '200') {
        commit('setCouponListByGs', data)
      }
    },
    // 获取非油品
    async ['getGoods'] ({commit}, id) {
      const { data: { data, code } } = await api.post('/v1/getGoods',
        {gsId: id,page:1,page_size:5})
      if (data && code === '200') {
        commit('setGoodslist', data)
      }
    },
    // 设置支付信息
    async ['pay'] ({commit}, config) {
      const { data: { data, code ,msg} } = await api.post('/v1/pay', config)
      if (data && code === '200') {
        commit('setPayinfo', data)
        if(data.pay_channel === 'balance_pay') {
          commit('TOAST_SHOW', {content: '购买成功', msg: true})
        }
      } else {
        commit('TOAST_SHOW', {content: msg, msg: true})
      }
    },
    // 根据油站ID获取用户信息
    async ['getMemberInfoByGsId'] ({commit}, id) {
      const { data: { data, code } } = await api.post('/v1/getMemberInfoByGsId',
        {openid: util.localItem.get('openid'),gsId:id})
      if (data && code === '200') {
        commit('setMemberInfoByGsId', data)
      }
    },
    // 购买成功评论页面
    async ['getContent'] ({commit}, config) {
      if (!config.orderNo) {
        commit('TOAST_SHOW', {content: '找不到订单编号', msg: true})
        return
      } else if(config.environmentLevel == '' || config.serviceLevel == '' || config.gasQualityLevel == ''){
        commit('TOAST_SHOW', {content: '请选择评分', msg: true})
        return
      } else if(config.content == ''){
        commit('TOAST_SHOW', {content: '评论不能为空', msg: true})
        return
      }
      const { data: { data, code } } = await api.post('/v1/comment', config)
      if (data && code === '200') {
        commit('TOAST_SHOW', {content: '提交成功', msg: true})
        location.reload()
      }
    },
    // 获取评论详情
    async ['getCommentShow'] ({commit}, orderNo) {
      const { data: { data, code ,msg } } = await api.post('/v1/getComment',
        {openid: util.localItem.get('openid'),orderNo:orderNo})
      if (data && code === '200') {
        commit('setComment', data)
      }
    }
  },
  getters: {
    ['getGasNoInfo'] (state) {
      return state.gasNoInfo
    },
    ['getRealPrice'] (state) {
      return state.RealPrice
    },
    ['getCouponListByGs'] (state) {
      return state.RealPrice
    },
    ['getCouponListByGs'] (state) {
      return state.Couponlist
    },
    ['getGoods'] (state) {
      return state.Goodslist.goods_list
    },
    ['getOrderNo'] (state) {
      return state.payinfo
    },
    ['getGasstationInfo'] (state) {
      return state.gasstationInfo
    },
    ['getMemberInfoByGsId'] (state) {
      return state.userInfo
    },
    ['getPayinfo'] (state) {
      return state.payinfo
    },
    ['getComment'] (state) {
      return state.commentShow
    }
  }
}
